/**
 * pharos的一次性任务
 */
const { connectWallet } = require('../apps/pharos/connectWallet');
const { addTestnet } = require('../apps/pharos/addTestnet');
const { check } = require('../apps/pharos/check');
const { swapTrade } = require('../apps/pharos/swapTrade');
const { swapPoll } = require('../apps/pharos/swapPoll');

async function main(browser, profileId) {
    // TODO:任务参数的一些请求
    const [page] = browser.pages();
    await connectWallet(browser, page);
    // await check(browser, page);
    // await addTestnet(browser, page);
    await swapPoll(browser, page);

}


// 测试代码
if (require.main === module) {
    async function launchAndConnectToBrowser(profileId) {
        const { chromium } = require('playwright');
        const host = 'localhost:8848';
        const apiKey = '7be6d900-d38c-4dcd-b2dd-45632a0266fc';
        const config = {
          headless: false, // support: true or false
          autoClose: true,
        };
        const query = new URLSearchParams({
          'x-api-key': apiKey, // required
          config: encodeURIComponent(JSON.stringify(config)),
        });
        const browserWSEndpoint = `ws://${host}/api/v2/connect/${profileId}?${query.toString()}`;
        console.log('browserWSEndpoint: ', browserWSEndpoint);
        const contexts = await chromium.connectOverCDP(browserWSEndpoint);
        const browser = contexts.contexts()[0];
        await main(browser, profileId);
      }
      launchAndConnectToBrowser('e1adca1c-2aa2-461b-9336-eb63f8c93dda').then();
}

module.exports = { main };